package cn.com.dhcc.turbo.web.controller.exch;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.turbo.entity.api.RouteLog;
import cn.com.dhcc.turbo.entity.exch.SendCmData;
import cn.com.dhcc.turbo.service.exch.cmdata.CmDataExchangeLifecycle;

@Controller
@RequestMapping("/exch/http_pipline")
public class HttpPipelineController extends BaseController {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	
	private static DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	@Qualifier(AppIocBean.CM_DATA_EXCHANGE_LIFECYCLE_BEAN)
	private CmDataExchangeLifecycle dataExchageLifecycle;

	/**
	 * 接收从目的端发过来的数据
	 * @param model
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/recv_cmdata", method = RequestMethod.POST)
	@ResponseBody
	public Status recvData(Model model, String data) {
		String msg = "";
		Status status = Status.getInstance();
		try {
			if (StrUtil.isBlank(data)) {
				status.setSuccess(false);
				status.setMsg("参数data不能为空");
				return status;
			}
			SendCmData cmData = JsonUtil.toObjectWithDateFormat(data, SendCmData.class);
			if (cmData == null) {
				status.setSuccess(false);
				status.setMsg("CmData数据转换失败，对象为空");
				return status;
			}
			msg = "传输管道[HttpTransportPipeline]接收数据成功";
			RouteLog.setRemotePipeline(cmData, AppConst.YES, msg, df1.format(new Date()));
			return dataExchageLifecycle.remoteStore(cmData);//数据在远程仓库入库
		} catch (Exception e) {
			msg = "传输管道[HttpTransportPipeline]接收数据出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);
			logger.error(msg, e);
			return status;
		}
	}

	/**
	 * 接收从目的端发过来的数据
	 * @param model
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/recv_send_status_and_routelog", method = RequestMethod.POST)
	@ResponseBody
	public Status recvSendStatusAndRoutelog(Model model, String data) {
		String msg = "";
		Status status = Status.getInstance();
		try {
			if (StrUtil.isBlank(data)) {
				status.setSuccess(false);
				status.setMsg("参数data不能为空");
				return status;
			}
			SendCmData cmData = JsonUtil.toObjectWithDateFormat(data, SendCmData.class);
			if (cmData == null) {
				status.setSuccess(false);
				status.setMsg("CmData数据转换失败，对象为空");
				return status;
			}
			return dataExchageLifecycle.updateSendStatusAndRoutelog(cmData);
		} catch (Exception e) {
			msg = "传输管道[HttpTransportPipeline]接收路由日志出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);
			logger.error(msg, e);
			return status;
		}
	}

	/**
	 * 接收从目的端发过来的数据
	 * @param model
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/recv_notify_status_and_routelog", method = RequestMethod.POST)
	@ResponseBody
	public Status recvNotifyStatusAndRoutelog(Model model, String data) {
		String msg = "";
		Status status = Status.getInstance();
		try {
			if (StrUtil.isBlank(data)) {
				status.setSuccess(false);
				status.setMsg("参数data不能为空");
				return status;
			}
			SendCmData cmData = JsonUtil.toObjectWithDateFormat(data, SendCmData.class);
			if (cmData == null) {
				status.setSuccess(false);
				status.setMsg("CmData数据转换失败，对象为空");
				return status;
			}
			return dataExchageLifecycle.updateNotifyStatusAndRoutelog(cmData);
		} catch (Exception e) {
			msg = "传输管道[HttpTransportPipeline]接收路由日志出现异常:" + e.fillInStackTrace();
			status.setSuccess(false);
			status.setMsg(msg);
			logger.error(msg, e);
			return status;
		}
	}
}
